<template>
  <div>
    <div class="app-container">
      <el-table v-loading="loading" :data="merchantList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="排名" align="center">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="商户名称" align="center" prop="merName"/>
        <el-table-column label="门店地址" align="center" prop="merAddress"/>
        <el-table-column label="负责人" align="center" prop="carMerchantPrincipal.pcpName"/>
        <el-table-column label="平均星级" align="center" prop="merRating">
          <template slot-scope="scope">
            <div>
              <el-rate
                :value="value=scope.row.merRating"
                :colors="colors">
              </el-rate>
            </div>
            <div style="color: red">
              {{scope.row.merRating}}星
            </div>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                  @pagination="getList"/>

    </div>

  </div>
</template>

<script>
import {listMerchant,listMerchant2, getMerchant, delMerchant, addMerchant, updateMerchant} from "@/api/carService/merchant";

export default {
  name: "Merchant",
  dicts: ['car_mer_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户管理表格数据
      merchantList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        merName: null,
        merAddress: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        merName: [
          {required: true, message: "店铺名称不能为空", trigger: "blur"}
        ],
        merArea: [
          {required: true, message: "经营区域不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  computed: {
    filteredDict() {
      return this.dict.type.car_mer_status.filter(item => item.value !== '0');
    },
    formattedAuditStatus() {
      switch (this.form.auditStatus) {
        case '0':
          return '未审核';
        case '1':
          return '已通过';
        case '2':
          return '已拒绝';
        default:
          return '未知状态';
      }
    }
  },
  methods: {
    /** 查询商户管理列表 */
    getList() {
      this.loading = true;
      listMerchant2(this.queryParams).then(response => {
        this.merchantList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        merName: null,
        merArea: null,
        registrationTime: null,
        startMerTime: null,
        stopMerTime: null,
        merAddress: null,
        longitude: null,
        latitude: null,
        merIntroduction: null,
        merRating: null,
        merStatus: null,
        deleteStatus: null,
        auditStatus: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        remark: null
      };
      this.resetForm("form");
    },



    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log("row==>start")
      console.log(row)
      console.log("row==>over")
      this.reset();
      const id = row.id || this.ids

      getMerchant(id).then(response => {
        this.form = response.data;
        this.open = true;
        console.log("row==>start")
        console.log(id)
        console.log("row==>over")
        this.title = "修改商户管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMerchant(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMerchant(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }

  }
};
</script>
<style scoped lang="scss">
<style>
